home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / tbl_indexes.lib.php < prev    next >
Encoding:
PHP Script  |  2004-11-02  |  11.2 KB  |  263 lines

  1. <?php
  2. /* $Id: tbl_indexes.lib.php,v 2.1 2004/11/02 13:41:30 garvinhicking Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.     /**
  6.      * Return a list of all index types
  7.      *
  8.      * @access  public
  9.      * @return  array       Index types
  10.      * @author  Garvin Hicking (pma@supergarv.de)
  11.      */
  12.  
  13.     function PMA_get_indextypes() {
  14.         return array(
  15.             'PRIMARY',
  16.             'INDEX',
  17.             'UNIQUE',
  18.             'FULLTEXT'
  19.         );
  20.     }
  21.  
  22.     /**
  23.      * Function to get all index information from a certain table
  24.      *
  25.      * @param   string      Table name
  26.      * @param   string      Error URL
  27.      *
  28.      * @access  public
  29.      * @return  array       Index keys
  30.      */
  31.     function PMA_get_indexes($tbl_name, $err_url_0 = '') {
  32.         $tbl_local_query = 'SHOW KEYS FROM ' . PMA_backquote($tbl_name);
  33.         $tbl_result      = PMA_DBI_query($tbl_local_query) or PMA_mysqlDie('', $tbl_local_query, '', $err_url_0);
  34.         $tbl_ret_keys    = array();
  35.         while ($tbl_row = PMA_DBI_fetch_assoc($tbl_result)) {
  36.             $tbl_ret_keys[]  = $tbl_row;
  37.         }
  38.         PMA_DBI_free_result($tbl_result);
  39.  
  40.         return $tbl_ret_keys;
  41.     }
  42.  
  43.     /**
  44.      * Function to check over array of indexes and look for common problems
  45.      *
  46.      * @param   array       Array of indexes
  47.      * @param   boolean     Whether to output HTML in table layout
  48.      *
  49.      * @access  public
  50.      * @return  string      Output HTML
  51.      * @author  Garvin Hicking (pma@supergarv.de)
  52.      */
  53.     function PMA_check_indexes($idx_collection, $table = true) {
  54.         $index_types = PMA_get_indextypes();
  55.         $output  = '';
  56.         if (is_array($idx_collection) && isset($idx_collection['ALL'])) {
  57.             foreach($idx_collection['ALL'] AS $w_keyname => $w_count) {
  58.                 if (isset($idx_collection['PRIMARY'][$w_keyname]) && (isset($idx_collection['INDEX'][$w_keyname]) || isset($idx_collection['UNIQUE'][$w_keyname]))) {
  59.                     $output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningPrimary'], htmlspecialchars($w_keyname)), $table);
  60.                 } elseif (isset($idx_collection['UNIQUE'][$w_keyname]) && isset($idx_collection['INDEX'][$w_keyname])) {
  61.                     $output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningUnique'], htmlspecialchars($w_keyname)), $table);
  62.                 }
  63.  
  64.                 foreach($index_types AS $index_type) {
  65.                     if (isset($idx_collection[$index_type][$w_keyname]) && $idx_collection[$index_type][$w_keyname] > 1) {
  66.                         $output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningMultiple'], $index_type, htmlspecialchars($w_keyname)), $table);
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.  
  72.         return $output;
  73.     }
  74.  
  75.     /**
  76.      * Loop array of returned index keys and extract key information to
  77.      * seperate arrays. Those arrays are passed by reference.
  78.      *
  79.      * @param   array       Referenced Array of indexes
  80.      * @param   array       Referenced return array
  81.      * @param   array       Referenced return array
  82.      * @param   array       Referenced return array
  83.      *
  84.      * @access  public
  85.      * @return  boolean     void
  86.      * @author  Garvin Hicking (pma@supergarv.de)
  87.      */
  88.     function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_data) {
  89.         if (!is_array($ret_keys)) {
  90.             return false;
  91.         }
  92.  
  93.         $prev_index   = '';
  94.         foreach ($ret_keys as $row) {
  95.             if ($row['Key_name'] != $prev_index ){
  96.                 $indexes[]  = $row['Key_name'];
  97.                 $prev_index = $row['Key_name'];
  98.             }
  99.  
  100.             $indexes_info[$row['Key_name']]['Sequences'][]     = $row['Seq_in_index'];
  101.             $indexes_info[$row['Key_name']]['Non_unique']      = $row['Non_unique'];
  102.  
  103.             if (isset($row['Cardinality'])) {
  104.                 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  105.             }
  106.  
  107.             //    I don't know what does following column mean....
  108.             //    $indexes_info[$row['Key_name']]['Packed']          = $row['Packed'];
  109.             $indexes_info[$row['Key_name']]['Comment']         = (isset($row['Comment']))
  110.                                                                ? $row['Comment']
  111.                                                                : '';
  112.             $indexes_info[$row['Key_name']]['Index_type']      = (isset($row['Index_type']))
  113.                                                                ? $row['Index_type']
  114.                                                                : '';
  115.  
  116.             $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name']  = $row['Column_name'];
  117.             if (isset($row['Sub_part'])) {
  118.                 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  119.             }
  120.         } // end while
  121.  
  122.         return true;
  123.     }
  124.  
  125.     /**
  126.      * Show index data and prepare returned collection array for index
  127.      * key checks.
  128.      *
  129.      * @param   string      The tablename
  130.      * @param   array       Referenced Array of indexes
  131.      * @param   array       Referenced info array
  132.      * @param   array       Referenced data array
  133.      * @param   boolean     Output HTML code, or just return collection array?
  134.      *
  135.      * @access  public
  136.      * @return  array       Index collection array
  137.      * @author  Garvin Hicking (pma@supergarv.de)
  138.      */
  139.     function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data, $display_html = true) {
  140.         $idx_collection = array();
  141.         foreach ($indexes AS $index_no => $index_name) {
  142.             if ($display_html) {
  143.                 $cell_bgd = (($index_no % 2) ? $GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo']);
  144.                 $index_td = '            <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
  145.                 echo '        <tr>' . "\n";
  146.                 echo $index_td
  147.                      . '                ' . htmlspecialchars($index_name) . "\n"
  148.                      . '            </td>' . "\n";
  149.             }
  150.  
  151.             if ((PMA_MYSQL_INT_VERSION < 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
  152.                 || (PMA_MYSQL_INT_VERSION >= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
  153.                 $index_type = 'FULLTEXT';
  154.             } else if ($index_name == 'PRIMARY') {
  155.                 $index_type = 'PRIMARY';
  156.             } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
  157.                 $index_type = 'UNIQUE';
  158.             } else {
  159.                 $index_type = 'INDEX';
  160.             }
  161.  
  162.             if ($display_html) {
  163.                 echo $index_td
  164.                      . '                ' . $index_type . "\n"
  165.                      . '            </td>' . "\n";
  166.  
  167.                 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
  168.                      . '                ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $GLOBALS['strNone']) . ' ' . "\n"
  169.                      . '            </td>' . "\n";
  170.  
  171.                 echo $index_td
  172.                      . '                <a href="tbl_indexes.php?' . $GLOBALS['url_query'] . '&index=' . urlencode($index_name) . '">' . $GLOBALS['edit_link_text'] . '</a>' . "\n"
  173.                      . '            </td>' . "\n";
  174.  
  175.                 if ($index_name == 'PRIMARY') {
  176.                     $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
  177.                     $js_msg      = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
  178.                     $zero_rows   = urlencode($GLOBALS['strPrimaryKeyHasBeenDropped']);
  179.                 } else {
  180.                     $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
  181.                     $js_msg      = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
  182.                     $zero_rows   = urlencode(sprintf($GLOBALS['strIndexHasBeenDropped'], htmlspecialchars($index_name)));
  183.                 }
  184.  
  185.                 echo $index_td
  186.                      . '                <a href="sql.php?' . $GLOBALS['url_query'] . '&sql_query=' . $local_query . '&zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $GLOBALS['drop_link_text']  . '</a>' . "\n"
  187.                      . '            </td>' . "\n";
  188.             }
  189.  
  190.             foreach ($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) {
  191.                 $col_name = $indexes_data[$index_name][$seq_index]['Column_name'];
  192.                 if ($row_no == 0) {
  193.                     if (isset($idx_collection[$index_type][$col_name])) {
  194.                         $idx_collection[$index_type][$col_name]++;
  195.                     } else {
  196.                         $idx_collection[$index_type][$col_name] = 1;
  197.                     }
  198.  
  199.                     if (isset($idx_collection['ALL'][$col_name])) {
  200.                         $idx_collection['ALL'][$col_name]++;
  201.                     } else {
  202.                         $idx_collection['ALL'][$col_name] = 1;
  203.                     }
  204.                 }
  205.  
  206.                 if ($display_html) {
  207.                     if ($row_no > 0) {
  208.                         echo '        <tr>' . "\n";
  209.                     }
  210.  
  211.                     if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
  212.                         echo '            <td bgcolor="' . $cell_bgd . '">' . "\n"
  213.                              . '                ' . $col_name . "\n"
  214.                              . '            </td>' . "\n";
  215.                         echo '            <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
  216.                              . '                ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
  217.                              . '            </td>' . "\n";
  218.                         echo '        </tr>' . "\n";
  219.                     } else {
  220.                         echo '            <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
  221.                              . '                ' . htmlspecialchars($col_name) . "\n"
  222.                              . '            </td>' . "\n";
  223.                         echo '        </tr>' . "\n";
  224.                     }
  225.                 }
  226.             } // end while
  227.         } // end while
  228.  
  229.         return $idx_collection;
  230.     }
  231.  
  232.     /**
  233.      * Function to emit a index warning
  234.      *
  235.      * @param   string      Message string
  236.      * @param   boolean     Whether to output HTML in table layout
  237.      *
  238.      * @access  public
  239.      * @output  string      Output HTML
  240.      * @author  Garvin Hicking (pma@supergarv.de)
  241.      */
  242.     function PMA_index_warning($string, $table = true) {
  243.         $output = '';
  244.         if ($table) {
  245.             $output .= "\n" . '        <tr><td colspan=7">' . "\n";
  246.         }
  247.  
  248.         if ($GLOBALS['cfg']['ErrorIconic']) {
  249.             $output .=  '<img src="' . $GLOBALS['pmaThemeImage'] . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />';
  250.         }
  251.  
  252.         $output .=  '        <b>' . $string . '</b>';
  253.  
  254.         if ($table) {
  255.             $output .=  '</td></tr>';
  256.         } else {
  257.             $output .=  '<br />';
  258.         }
  259.  
  260.         $output .=  "\n\n";
  261.         return $output;
  262.     }
  263. ?>